import os
import model as M
import json
from django.contrib.gis.gdal import DataSource
from xml.dom import minidom



class ShpReader:
    """
    Shape file reader.
    """

    def __init__(self):
        self.path = ''


def geojson_to_geometry(document, geojson):
    geo = M.Geometry(document)
    if geojson['type'] == 'Polygon':
        for coords in geojson['coordinates']:
            point_set = M.PointSet(document)
            for p in coords:
                point = M.Point(document, p[0], p[1])
                point_set.add_point(point)
            geo.add_child(point_set)
    return geo


if __name__ == '__main__':
    shp_path = os.path.join('/home/vivien/code/HDMapOpenDrive/layers', 'lane.shp')
    ds = DataSource(shp_path)
    layer = ds[0]
    xml = minidom.Document()
    for feature in layer:
        point_set = M.PointSet(xml)
        geojson = json.loads(feature.geom.geojson)
        geo = geojson_to_geometry(xml, geojson)
